package com.tlgen.modules.drugstore.service.impl;

import com.tlgen.common.exception.EntityExistException;
import com.tlgen.common.utils.FileUtil;
import com.tlgen.common.utils.PageUtil;
import com.tlgen.common.utils.QueryHelp;
import com.tlgen.common.utils.ValidationUtil;
import com.tlgen.modules.drugstore.domain.MedicinalSort;
import com.tlgen.modules.drugstore.repository.MedicinalSortRepository;
import com.tlgen.modules.drugstore.service.MedicinalSortService;
import com.tlgen.modules.drugstore.service.dto.MedicinalSortDto;
import com.tlgen.modules.drugstore.service.dto.MedicinalSortQueryCriteria;
import com.tlgen.modules.drugstore.service.mapper.MedicinalSortMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Service
@CacheConfig(cacheNames = "MedicinalSort")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MedicinalSortServiceImpl implements MedicinalSortService {

    private final MedicinalSortRepository medicinalSortRepository;

    private final MedicinalSortMapper medicinalSortMapper;

    public MedicinalSortServiceImpl(MedicinalSortRepository medicinalSortRepository, MedicinalSortMapper medicinalSortMapper) {
        this.medicinalSortRepository = medicinalSortRepository;
        this.medicinalSortMapper = medicinalSortMapper;
    }

    /**
     * 根据登记人员名称获取信息
     * @param name
     * @return
     */
    @Override
    @Cacheable(key = "'loadUserByName:'+#p0")
    public MedicinalSortDto findByName(String name) {
        MedicinalSort MedicinalSort1;
        MedicinalSort1 = medicinalSortRepository.findByMedicinalSortName(name);
        if(MedicinalSort1==null){
            throw new EntityExistException(MedicinalSort.class,"MedicinalSort",name);
        }else {
            return medicinalSortMapper.toDto(MedicinalSort1);
        }
    }

    /**
     * 不带条件分页查询
     * @param pageable 分页参数
     * @return
     */
    @Override
    @Cacheable
    public Object queryAll(Pageable pageable) {
        return medicinalSortMapper.toDto(medicinalSortRepository.findAll(pageable).getContent());
    }


    /**
     * 带条件分页查询
     * @param criteria 条件
     * @param pageable 分页参数
     * @return
     */
    @Override
    @Cacheable
    public Object queryAll(MedicinalSortQueryCriteria criteria, Pageable pageable) {
        Page<MedicinalSort> page = medicinalSortRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(medicinalSortMapper::toDto));
    }

    /**
     * 带条件不分页查询
     * @param criteria 条件
     * @return
     */
    @Override
    @Cacheable
    public List<MedicinalSortDto> queryAll(MedicinalSortQueryCriteria criteria) {
        return medicinalSortMapper.toDto(medicinalSortRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    /**
     * 新增
     * @param resources /
     * @return
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public MedicinalSortDto create(MedicinalSort resources) {
        if (medicinalSortRepository.findByMedicinalSortName(resources.getMedicinalSortName()) != null) {
            throw new EntityExistException(MedicinalSort.class, "MedicinalSort", resources.getMedicinalSortName());
        }
        return medicinalSortMapper.toDto(medicinalSortRepository.save(resources));
    }

    /**
     * 编辑
     * @param resources /
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(MedicinalSort resources) {
        MedicinalSort medicinalSort = medicinalSortRepository.findById(resources.getId()).orElseGet(MedicinalSort::new);
        ValidationUtil.isNull(medicinalSort.getId(),"MedicinalSort","id",resources.getId());

        medicinalSort.setMedicinalSortName(resources.getMedicinalSortName());
        medicinalSortRepository.save(medicinalSort);
    }

    /**
     * 删除
     * @param ids /
     */
    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(Set<Long> ids) {
        for (Long id : ids) {
            medicinalSortRepository.deleteById(id);
        }
    }

    /**
     * 导出数据
     * @param response /
     * @throws IOException
     */
    @Override
    public void download(List<MedicinalSortDto> medicinalSortDtos, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MedicinalSortDto medicinalSortDto : medicinalSortDtos) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("药名分类名", medicinalSortDto.getMedicinalSortName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 根据Id查询
     * @param id /
     * @return
     */
    @Override
    @Cacheable(key = "#p0")
    public MedicinalSortDto findById(long id) {
        MedicinalSort medicinalSort = medicinalSortRepository.findById(id).orElseGet(MedicinalSort::new);
        ValidationUtil.isNull(medicinalSort.getId(),"MedicinalSort","id",id);
        return medicinalSortMapper.toDto(medicinalSort);
    }

}
